//
//  BIDNoticeDetailViewController.m
//  cxgov
//
//  Created by mal on 16/3/6.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDSentLeaveDetailViewController.h"
#import "BIDReceiveConditionForLeaveViewController.h"
#import "BIDMenuView.h"

//删除
static NSString *deleteLeaveURL = @"leaveAndroid!dropLeave.action";

//查看申请详情
static NSString *leaveDetailURL = @"leaveAndroid!toReadLeave.action";

@interface BIDSentLeaveDetailViewController ()<BIDMenuViewDelegate, UIAlertViewDelegate>
{
    BOOL _bShowMenu;
    BIDMenuView *_menuView;
}
@end

@implementation BIDSentLeaveDetailViewController
@synthesize leaveId;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"通知详情";
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_more.png"] style:UIBarButtonItemStylePlain target:self action:@selector(functionHandler)];
    //
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated
{
}
//加载通知详情
- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], leaveDetailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@", leaveId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            NSString *strCreateDate = responseDic[@"createTime"];
            NSString *strNoticeContent = responseDic[@"title"];
            _dateLabel.text = strCreateDate;
            _contentTextView.text = strNoticeContent;
            //
            CGSize screenSize = [UIScreen mainScreen].bounds.size;
            CGFloat fontSize = [AppDelegate getFontSize];
            _contentTextView.font = [UIFont systemFontOfSize:fontSize];
            CGFloat contentHeight = [BIDGlobalMethods getHeightWithString:strNoticeContent font:[UIFont systemFontOfSize:fontSize] constraintSize:CGSizeMake(CGRectGetWidth(self.view.frame)-8*2, MAXFLOAT)];
            if(contentHeight>CGRectGetHeight(_contentTextView.frame))
            {
                CGFloat viewHeight = _heightForContainerViewConstraint.constant + (contentHeight-CGRectGetHeight(_contentTextView.frame));
                if((viewHeight+30)>screenSize.height) viewHeight = screenSize.height-64-30;
                _heightForContainerViewConstraint.constant = viewHeight;
                [self.view layoutIfNeeded];
            }
        }
    }];
}

/**
 *  更多
 */
- (void)functionHandler
{
    //弹出菜单
    if(!_menuView)
    {
        NSArray *menuNameArr = @[@" 接收情况", @" 删除        "];
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        //menuWidth,menuHeight,triangleWidth,triangleHeight,triangleToRight
        BIDMenuAttribute menuAttribute = {120, 5, 10, 15, 14};
        int menuViewPosX = screenSize.width - 26 - (menuAttribute.menuWidth-menuAttribute.triangleToRight-menuAttribute.triangleWidth/2);
        int menuViewPosY = 2;
        NSArray *imgNameArr = @[@"menu_receive_condition.png", @"menu_delete.png"];
        _menuView = [[BIDMenuView alloc] initWithFrame:CGRectMake(menuViewPosX, menuViewPosY, menuAttribute.menuWidth, menuAttribute.menuHeight) menuNameArr:menuNameArr imgNameArr:nil menuAttribute:menuAttribute];
        _menuView.delegate = self;
    }
    if(!_bShowMenu)
    {
        _bShowMenu = YES;
        [self.view addSubview:_menuView];
    }
    else
    {
        _bShowMenu = NO;
        [UIView animateWithDuration:1.0f animations:^{
            //_menuView.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
        } completion:^(BOOL bFinished){
            if(bFinished)
            {
                [_menuView removeFromSuperview];
            }
        }];
    }
}

//删除列表中的一条通知

- (void)deleteNotice
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], deleteLeaveURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"ids=%@", leaveId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            NSNumber *value = responseDic[@"isSuccess"];
            if([value intValue]==1)
            {
                [BIDGlobalMethods showAlertView:@"删除成功" buttonTitle:ALERT_BTN_TITLE delegate:self tag:1];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"删除失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}

#pragma mark - BIDMenuViewDelegate
- (void)selectMenuAtIndex:(int)index
{
    _bShowMenu = NO;
    switch(index)
    {
        case 0:
        {
            //接收情况
            BIDReceiveConditionForLeaveViewController *vc = [[BIDReceiveConditionForLeaveViewController alloc] init];
            vc.leaveId = leaveId;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            //删除
            [self deleteNotice];
        }
            break;
    }
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 1)
    {
        //删除成功
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
